#encoding=utf-8
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from PyPDF2 import PdfFileMerger
import os

def jpg2pdf(f_jpg, f_pdf):
    w_pdf = 21*cm
    h_pdf = 29.7*cm
                                                                                           
    c = canvas.Canvas(f_pdf, pagesize = (w_pdf, h_pdf))
    c.setFillAlpha(1)
    print c.drawImage(f_jpg, 0, 0, w_pdf, h_pdf)
    c.save()

def jpgs2pdf(imgs, pdf):
    merger = PdfFileMerger()
    for i, img_path in enumerate(imgs):
        pdf_path = 'pdfs/{}.pdf'.format(i+1)
        jpg2pdf(img_path, pdf_path)
        # merge
        merger.append(open(pdf_path, 'rb'))
    
    merger.write(open(pdf, 'wb'))


